//
//  XJDiscoveryView.swift
//  新浪微博
//
//  Created by 徐绩 on 16/4/16.
//  Copyright © 2016年 ayumi. All rights reserved.
//

import UIKit

class XJSearchView: UIView {

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var textFieldRightConstraint: NSLayoutConstraint!
    
    
    /**
     类方法加载xibs视图
     */
    class func searchView() -> XJSearchView {
        
        
        return NSBundle.mainBundle().loadNibNamed("XJSearchView", owner: nil, options: nil).last! as! XJSearchView
    }
    
    
    /**
     从xib里面唤醒,里面的子控件全部连线完毕,设置leftView
     */
    override func awakeFromNib() {
        
        leftView.frame.size = CGSize(width: self.frame.height, height: self.frame.height)
        textField.leftView = leftView
        textField.leftViewMode = .Always
        
    }
    
    
    //textField开始编辑
    @IBAction func editingBegin(sender: UITextField) {
        
        //改约束
        textFieldRightConstraint.constant = cancelBtn.frame.width
        
        //执行动画
        UIView.animateWithDuration(0.5) { () -> Void in
            
            //重新布局
            self.layoutIfNeeded()
        }
    }
    
    //点击取消按钮
    @IBAction func cancelBtnClick(sender: AnyObject) {
        
        textFieldRightConstraint.constant = 0
        
        UIView.animateWithDuration(0.5) { () -> Void in
            
            self.layoutIfNeeded()
        }
        self.textField.resignFirstResponder()
    }
    
    /**
    *  懒加载放大镜图片
    */
    private lazy var leftView:UIImageView = {
    
    let imageView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        //设置内容模式
        imageView.contentMode = .Center
        
        return imageView
    }()
    
    
}
